package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址管理接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址簿
     *
     * @return
     */
    @ApiOperation("新增地址簿")
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook) {
        addressBookService.add(addressBook);
        return Result.success();
    }

    /**
     * 查看当前用户的地址簿
     *
     * @return
     */
    @ApiOperation("查看当前用户地址簿")
    @GetMapping("/list")
    public Result<List<AddressBook>> findByUserId() {
        List<AddressBook> list = addressBookService.findByUserId();
        return Result.success(list);
    }

    /**
     * 设置当前地址为默认地址
     *
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestParam(defaultValue = "1") Integer isDefault, Integer id) {
        addressBookService.setDefault(isDefault, id);
        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result findDefault() {

        AddressBook addressBook= addressBookService.findDefault();

        if(addressBook!=null){
            return Result.success(addressBook);
        }
        return Result.error("没有查询到默认地址");
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }

}
